home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 June / Macworld (1999-06).dmg / Shareware World / Info / For Developers / MacZoop2.0.sea / MacZoop2.0 / Required Classes / ZTimer.cpp < prev    next >
Text File  |  1999-02-11  |  4KB  |  218 lines

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            MacZoop - "the framework for the rest of us"         
  5. *
  6. *
  7. *
  8. *            ZTimer.cpp            -- simple timer object
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1998, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22. #include    "ZTimer.h"
  23. #include    "ZWindow.h"
  24. #include    "MacZoop.h"
  25.  
  26.  
  27. static long                gTimerIDSeed = 20000;
  28. static ZTimerQueue*        gTimerQ = NULL;
  29. static unsigned long    gTickCount = 0;
  30.  
  31.  
  32.  
  33. void        TimerTimer();
  34.  
  35.  
  36. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  37.  
  38.  
  39. ZTimer::ZTimer( unsigned long tRate, long anID, ZCommander* owner, Boolean isOneShot )
  40.     : ZComrade()
  41. {
  42.     wOwner = owner;
  43.     id = anID;
  44.     oneShot = isOneShot;
  45.     
  46.     if ( id == 0 )
  47.         id = ++gTimerIDSeed;
  48.         
  49.     interval = tRate;
  50.     lastTicks = gTickCount;
  51. }
  52.     
  53.  
  54.  
  55. /*------------------------------------***  DO  ***--------------------------------------*/
  56. /*    
  57.  
  58. check if time to fire and call window or broadcast message
  59. ----------------------------------------------------------------------------------------*/
  60.  
  61. void    ZTimer::Do()
  62. {
  63.     // check if time has elapsed or if tickcount has wrapped...
  64.     
  65.     if ( gTickCount >= ( lastTicks + interval ) || ( gTickCount < lastTicks ))
  66.     {
  67.         lastTicks = gTickCount;
  68.         
  69.         if ( wOwner )
  70.         {
  71.             // if the owner's a window, focus it.
  72.             
  73.             if ( dynamic_cast<ZWindow*>( wOwner ))
  74.                 ((ZWindow*) wOwner )->Focus();
  75.             
  76.             wOwner->DoTimer( id );
  77.         }
  78.         else
  79.             SendMessage( kTimerMsgTimerTripped, (void*) id );
  80.         
  81.         // if one shot, destroy after firing
  82.         
  83.         if ( oneShot )
  84.         {
  85.             // delete from list
  86.             
  87.             gTimerQ->DeleteObject( this );
  88.             
  89.             if ( gTimerQ->CountItems() <= 0 )
  90.                 ForgetObject( gTimerQ );
  91.             
  92.             ForgetThis();
  93.         }
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. /*----------------------------------***  SETTIMER  ***----------------------------------*/
  100. /*    
  101.  
  102. creates timer object and installs it into the timer queue
  103. ----------------------------------------------------------------------------------------*/
  104.  
  105. ZTimer*        SetTimer( ZCommander* aCmdr, long id, unsigned long interval, Boolean isOneShot )
  106. {
  107.     ZTimer*        zt;
  108.     
  109.     if ( gTimerQ == NULL )
  110.         FailNIL( gTimerQ = new ZTimerQueue());
  111.  
  112.     FailNIL( zt = new ZTimer( interval, id, aCmdr, isOneShot ));
  113.     
  114.     gTimerQ->AppendItem( zt );
  115.     
  116.     return zt;
  117. }
  118.  
  119.  
  120. /*---------------------------------***  KILLTIMER  ***----------------------------------*/
  121. /*    
  122.  
  123. removes timer from the queue and discards it
  124. ----------------------------------------------------------------------------------------*/
  125.  
  126. void        KillTimer( ZCommander* aCmdr, long id )
  127. {
  128.     long        iMax;
  129.     ZTimer*        zt;
  130.     
  131.     if ( gTimerQ )
  132.     {
  133.         iMax = gTimerQ->CountItems();
  134.         
  135.         while ( iMax )
  136.         {
  137.             zt = (ZTimer*) gTimerQ->GetObject( iMax-- );
  138.             
  139.             if ( zt && ( zt->GetID() == id ) && ( zt->GetOwner() == aCmdr ))
  140.             {
  141.                 gTimerQ->DeleteObject( zt );
  142.                 ForgetObject( zt );
  143.                 
  144.                 break;
  145.             }
  146.         }
  147.         
  148.         if ( gTimerQ->CountItems() <= 0 )
  149.             ForgetObject( gTimerQ );
  150.     }
  151. }
  152.  
  153.  
  154.  
  155. /*-------------------------------***  KILLALLTIMERS  ***--------------------------------*/
  156. /*    
  157.  
  158. removes and deletes all timers associated with a particular window. Called when a window
  159. is destructed.
  160. ----------------------------------------------------------------------------------------*/
  161.  
  162. void        KillAllTimers( ZCommander* aCmdr )
  163. {
  164.     long        iMax;
  165.     ZTimer*        zt;
  166.  
  167.     if ( gTimerQ )
  168.     {
  169.         iMax = gTimerQ->CountItems();    
  170.     
  171.         while ( iMax )
  172.         {
  173.             zt = (ZTimer*) gTimerQ->GetObject( iMax-- );
  174.         
  175.             if ( zt && zt->GetOwner() == aCmdr )
  176.             {
  177.                 gTimerQ->DeleteObject( zt );
  178.                 ForgetObject( zt );
  179.             }
  180.         }
  181.         
  182.         if ( gTimerQ->CountItems() <= 0 )
  183.             ForgetObject( gTimerQ );
  184.     }
  185. }
  186.  
  187.  
  188. /*---------------------------------***  TIMERTIMER  ***---------------------------------*/
  189. /*    
  190.  
  191. called repeatedly by ZApplication to iterate the timer queue and trigger timers. Note that
  192. this iterates in reverse so that if a timer responder deletes the timer, it will continue
  193. to trigger the remaining timers.
  194. ----------------------------------------------------------------------------------------*/
  195.  
  196. void        TimerTimer()
  197. {
  198.     // copy the tickcount to a global so the iteration can proceed as fast as possible
  199.     
  200.     gTickCount = TickCount();
  201.     
  202.     long        iMax;
  203.     ZTimer*        zt;
  204.     
  205.     if ( gTimerQ )
  206.     {
  207.         iMax = gTimerQ->CountItems();
  208.         
  209.         while( iMax )
  210.         {
  211.             zt = (ZTimer*) gTimerQ->GetObject( iMax-- );
  212.             
  213.             if ( zt )
  214.                 zt->Do();
  215.         }
  216.     }
  217. }
  218.